﻿using System;
using System.Collections.Generic;
using System.Text;
using Toyger.Common.PlugIn;
using Toyger.Common.Exceptions;
using Toyger.Common.Connections;

namespace Toyger.SystemControl
{
    class PlugInsManager
    {
        static Dictionary<String, IDBManager> servers = new Dictionary<string, IDBManager>(StringComparer.OrdinalIgnoreCase);
        internal static ConnectionSession CreateSession(ConnectionObject cObj)
        {
               return  GetServer(cObj.ServerType).NewSession(cObj);
        }

        internal static void RegisteDBManager(string servertype, IDBManager dbManager)
        {
            lock (servers)
            {
                if (servers.ContainsKey(servertype))
                {
                    servers[servertype] = dbManager;
                }
                else servers.Add(servertype, dbManager);
            }
        }

        internal static IDBManager GetServer(string serverType)
        {
            lock (servers)
            {
                IDBManager df = null;
                if (servers.TryGetValue(serverType, out df))
                {
                    return df;
                }
                else throw new ServerNotExistsException(serverType);
            }
        }

        /// <summary>
        /// Get the list of all avaliable server types.
        /// </summary>
        /// <returns></returns>
        internal static string[] GetServerList()
        {
            return servers.Keys.ToArray<String>();
        }
    }
}
